﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EarringsRecommendationWeb
{
    public partial class Home : System.Web.UI.Page
    {
        protected  void Page_Load(object sender, EventArgs e)
        {
            Member mem = Session["member"] as Member;

            if (mem != null)
            {
                Response.Write("<input id='hidden-email' type='hidden' value='" + mem.Email + "'>");
                Response.Write("<input id='hidden-name' type='hidden' value='" + mem.Name + "'>");
            }

            List<RecalQty> listQty = Session["RecalQty"] as List<RecalQty>;
            int qty = 0;
            if (listQty != null)
            {
                foreach (RecalQty item in listQty)
                {
                    qty += Convert.ToInt32(item.Qty);
                }
            }
            ClientScript.RegisterArrayDeclaration("qty", string.Format("\"{0}\"", qty));

            if (!string.IsNullOrEmpty(Request.QueryString["style"]))
            {
                string style = Request.QueryString["style"].ToString();

                List < EarringsMaster >  earringsResult = this.GetEarrings(style);
                ClientScript.RegisterArrayDeclaration("Earrings", JsonConvert.SerializeObject(earringsResult));
            }
        }

        private List<EarringsMaster> GetEarrings(string style)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT er.er_code,er.er_name,er.er_detail");
            strSql.Append(" ,er.er_price,er.er_image_front,erconf.config_name");
            strSql.Append(" FROM earrings er");
            strSql.Append(" INNER JOIN  earringsconfig erconf ON er.er_style = erconf.config_id");
            strSql.Append(" WHERE erconf.config_name = '").Append(style).Append("'");

            List<EarringsMaster> earringsResult = new List<EarringsMaster>();
            using (SqlDataReader value = SqlClientUtility.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (value.Read())
                {
                    string code = Convert.ToString(value["er_code"]);
                    string name = Convert.ToString(value["er_name"]);
                    string detail = Convert.ToString(value["er_detail"]);
                    double price = Convert.ToDouble(value["er_price"]);
                    string imgPath = "Data/Earrings/" + Convert.ToString(value["config_name"]) + "/" + Convert.ToString(value["er_image_front"]);

                    EarringsMaster pEarrings = new EarringsMaster(code, name, detail, price, imgPath,"","","","");
                    earringsResult.Add(pEarrings);
                }
            }            
            return earringsResult;
        }
    }
}